#include <iostream>
using namespace std;

void swap(int a[], int i, int j) {
    int tmp = a[i];
    a[i] = a[j];
    a[j] = tmp;
}

void solve() {
    int n;
    cin >> n;

    long long sum = 0;
    for (int i = 1; i <= n - 1; i++)
        sum += 1LL * i * i;

    cout << sum << endl;

    int p[n];
    for (int i = 0; i < n; i++)
        p[i] = i + 1;
    for (int i = 1; i < n; i++)
        swap(p, n - 1, n - 1 - i);

    for (int i = 0; i < n; i++)
        cout << p[i] << " \n"[i == n - 1];

    cout << n - 1 << endl;

    for (int i = 1; i <= n - 1; i++) {
        cout << i << " " << n << endl;
    }
}

int main() {
    int t;
    cin >> t;
    while (t-- > 0)
        solve();
    return 0;
}